package com.itszt.psqueue;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import wjw.psqueue.client.PSQueueClient;
import wjw.psqueue.msg.*;

import static org.junit.Assert.*;

/**
 * Created by Administrator--善知  on 2018/6/26.
 */
public class Test1 {
    //我们将来请求的队列名称
    String queue_name = "test_queue_java9";
    //消费者名称
    String sub_name = "test_sub";

    String user = "admin";
    String pass = "123456";
    //队列连接实例
    PSQueueClient instance = new PSQueueClient("127.0.0.1", 1818, "UTF-8", 60 * 1000, 60 * 1000);

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void listAllQueue() {
        ResList resList = instance.queueNames();
        System.out.println("resList = " + resList);
    }

    //创建一个队列
    @Test
    public void test_createQueue() {
        ResultCode rest = instance.createQueue(queue_name, 10000000L, user, pass);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }
    //修改队列上限容量
    @Test
    public void test_setQueueCapacity() {
        ResultCode rest = instance.setQueueCapacity(queue_name, 10000000L, user, pass);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }
    //创建消费者
    @Test
    public void test_createSub() {
        ResultCode rest = instance.createSub(queue_name, sub_name, user, pass);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }

    //查看指定队列拥有的消费者
    @Test
    public void getQueueSub() {
        ResList resList = instance.subNames(queue_name);
        System.out.println("resList = " + resList);
    }

    @Test
    //数据入队列
    public void test_add() {
        ResAdd rest = instance.add(queue_name, "你好PSQueue3333（将来这是一个json，这个json描述了我们的请求）");
        assertEquals(ResultCode.SUCCESS.code, rest.status.code);
    }

    //查看，但是并不取出
    @Test
    public void test_view() {
        ResData rest = instance.view(queue_name, 0);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.status.code);
    }

    @Test
    //数据出队列
    public void test_poll() {
        ResData rest = instance.poll(queue_name, sub_name);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.status.code);
    }

    //销毁某个消费者
    @Test
    public void test_removeSub() {
        ResultCode rest = instance.removeSub(queue_name, sub_name, user, pass);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }

    //销毁队列
    @Test
    public void test_removeQueue() {
        ResultCode rest = instance.removeQueue(queue_name, user, pass);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }

    //查看队列状态
    @Test
    public void test_status() {
        ResQueueStatus rest = instance.status(queue_name);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.status.code);
    }

    //查看消费者状态
    @Test
    public void test_statusForSub() {
        ResSubStatus rest = instance.statusForSub(queue_name, sub_name);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.status.code);
    }

    //重置队列
    @Test
    public void test_resetQueue() {
        ResultCode rest = instance.resetQueue(queue_name, user, pass);
        System.out.println("rest = " + rest);
        assertEquals(ResultCode.SUCCESS.code, rest.code);
    }


}
